import { Component, OnInit, ViewChild } from '@angular/core';
import { Router} from '@angular/router';
import { HeroService } from '../../../../services/hero.service';
import { NzFormatEmitEvent, NzTreeNode, NzTreeNodeOptions } from 'ng-zorro-antd';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-addrole',
  templateUrl: './addrole.component.html',
  styleUrls: ['./addrole.component.css']
})
export class AddroleComponent implements OnInit {

  @ViewChild('treeCom') treeCom;
  btn_loading=false;
  public nameCode:number;
  public profileCode:number;
  public keysCode:number;
  public name:string;
  public profile:string;
  public menulist:any;
  public nodes:Array<any>=[];
  public keys:Array<any>=[];
  constructor(private heroService: HeroService,
              private router: Router,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
    // 获取树状菜单
    let url=host+"/main/roles/add";
    this.heroService.get(url).then(result=>{
        // console.log(result.data);
        this.menulist=result.data.actions;
        this.nodes=[];
        for(let i=0;i<this.menulist.length;i++){
          if(this.menulist[i].level==0){
            this.nodes.push({"title":this.menulist[i].name,"key":this.menulist[i].id,isLeaf:true});
          }
        }
        
        for(let j=0;j<this.menulist.length;j++){
          if(this.menulist[j].level==1){
              for(let k=0;k<this.nodes.length;k++){
                if(this.menulist[j].pid==this.nodes[k].key){
                  delete this.nodes[k].isLeaf;
                  if(this.nodes[k].children){
                    this.nodes[k].expanded="false";
                    this.nodes[k].children.push({"title":this.menulist[j].name,"key":this.menulist[j].id,isLeaf:true});
                  }else{
                    this.nodes[k].children=[];
                    this.nodes[k].expanded="false";
                    this.nodes[k].children.push({"title":this.menulist[j].name,"key":this.menulist[j].id,isLeaf:true});
                  }

                }
              }
          }
        }
        //第三层遍历
        for(let m=0;m<this.menulist.length;m++){
          if(this.menulist[m].level==2){
              for(let n=0;n<this.nodes.length;n++){
                if(this.nodes[n].children){
                  for(let x=0;x<this.nodes[n].children.length;x++){
                    if(this.menulist[m].pid==this.nodes[n].children[x].key){
                      delete this.nodes[n].children[x].isLeaf;
                      if(this.nodes[n].children[x].children){
                        this.nodes[n].children[x].expanded="false";
                        this.nodes[n].children[x].children.push({"title":this.menulist[m].name,"key":this.menulist[m].id,isLeaf:true});
                      }else{
                        this.nodes[n].children[x].children=[];
                        this.nodes[n].children[x].expanded="false";
                        this.nodes[n].children[x].children.push({"title":this.menulist[m].name,"key":this.menulist[m].id,isLeaf:true});
                      }
                    }
                  }
                }
              }
          }
        }
        // console.log(this.nodes);
    })
  }

  nzCheck(event: NzFormatEmitEvent): void {
    console.log(event.nodes);
    this.keys=event.keys;
    // 选中子菜单附带把父菜单提交
    for(let i in event.nodes){
      if(event.nodes[i].parentNode){
        this.keys.push(event.nodes[i].parentNode.key);
        if(event.nodes[i].parentNode.parentNode){
          this.keys.push(event.nodes[i].parentNode.parentNode.key);
        }
      }
      if(event.nodes[i].children.length > 0){
        for(let k in event.nodes[i].children){
          this.keys.push(event.nodes[i].children[k].key);
          if(event.nodes[i].children[k].children.length > 0){
            for(let f in event.nodes[i].children[k].children){
              this.keys.push(event.nodes[i].children[k].children[f].key);
            }
          }
        }
      }
    }
    this.keys=Array.from(new Set(this.keys));
    // console.log(this.keys);
    this.keysCode=0;
  }

  //提交
  submitRole(){
    if(!this.name){
      this.nameCode=-1;
    }
    if(!this.profile){
      this.profileCode=-1;
    }
    console.log(this.keys);
    if(this.keys.length <1){
      this.keysCode=-1;
      this.message.warning("请选择权限");
    }
    if(this.name && this.profile && this.keys.length >0){
      this.btn_loading=true;
      let url=host+"/main/roles/save";
      let model='name='+this.name+'&profile='+this.profile+'&actionsIds='+this.keys;
      this.heroService.post(url,model).then(result=>{
        // console.log(result.code);
        if(result.code==0){
          this.btn_loading=false;
          this.message.success('设置成功');
          this.router.navigate(['/main/roles']);
        }else{
          this.btn_loading=false;
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
      })
    }
  }
  nameOnFocus(){
    this.nameCode=0;
  }
  profileOnFocus(){
    this.profileCode=0;
  }
}
